make_one_table<-function(mynet,patient0,vaccinated,vulnerable){
    if(missing(vaccinated)) {
     vaccinated=NULL
    }
  n=nrow(as.matrix(mynet))
  round1<-setdiff(names(as.matrix(mynet)[patient0,][as.matrix(mynet)[patient0,]==1]),vaccinated)

  mynet1<-as.matrix(mynet)
    if(!missing(vaccinated)) {
      mynet1[vaccinated,]<-0
      mynet1[,vaccinated]<-0
    }
  g.sim2<-as.matrix(mynet1)%*%as.matrix(mynet1)
  diag(g.sim2)<-0
  round2<-names(as.matrix(g.sim2)[patient0,][as.matrix(g.sim2)[patient0,]>0])
  round2<-setdiff(round2,round1)

  infected_num<-length(round1)+length(round2)
  vaccinated_num<-length(vaccinated)
  notinfected_num<-n-infected_num-vaccinated_num-1

  notinfected<-setdiff(row.names(as.matrix(mynet)),c(round1,round2,vaccinated,patient0))
  inf_vulnerable<-intersect(vulnerable,c(round1,round2))
  inf_vulnerable_num<-length(inf_vulnerable)

  if (length(inf_vulnerable_num)==0){
    inf_vulnerable_num<-0
  }


  ggraph<-igraph::graph_from_adjacency_matrix(as.matrix(mynet), weighted=NULL, mode="undirected",
                                      diag=FALSE)
  #med_inf<-median(igraph::degree(ggraph)[c(round1,round2)])
                  if(!missing(vaccinated)) {
  #med_vac<-median(igraph::degree(ggraph)[vaccinated])
                  } else {med_vac= NA}
  #med_ni<-median(igraph::degree(ggraph)[notinfected])
  if(length(inf_vulnerable_num)!=0) {
    med_infv<-median(igraph::degree(ggraph)[inf_vulnerable])
  } else {med_infv= NA}
#  rbind(c(infected_num,vaccinated_num,notinfected_num,inf_vulnerable_num),c(med_inf,med_vac,med_ni,med_infv))
  cbind.data.frame(infected_num,vaccinated_num,notinfected_num,inf_vulnerable_num)
  }


combine_tabs<-function(vaccinated=vac_scenario,mynet,patient0, vulnerable){
    if (missing(vaccinated)){
      vaccinated=NULL
          }
  mytab<-make_one_table(mynet=mynet,patient0=patient0,vulnerable,vaccinated=vaccinated)
  colnames(mytab) <- c("Infected", "Vaccinated", "Not Infected","Vulnerable Infected")
#  mytab<-cbind(c("Number of Cases","Median Degree"),mytab)
  return(mytab)
}

run_combine_tabs<-function(mynet,patient0,vaccinated=vaccinated,vulnerable=vulnerable){
  combine_tabs(vaccinated=vaccinated,mynet,patient0,vulnerable)
}

#run_combine_tabs(mynet,patient0,vaccinated=vac_scenario)

